/** 
 * @Title: Cos.java 
 * @Package word2vec 
 * @Description: TODO
 * @author HeYan
 * @date 2016年10月8日 下午7:10:09 
 */
package word2vec.cos;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.zizo.util.ConnectionPool;

/**
 * @ClassName: Cos
 * @Description: TODO 求2个词之间的余弦值
 * @author HeYan
 * @date 2016年10月8日 下午7:10:09
 * 
 */
public class CosDao {
	private final static Connection conn = ConnectionPool.getConnection("word2vec", "localhost");

	public void getVec() {
		Statement st = null;
		ResultSet rs = null;
		List<Cos> coss = new ArrayList<>();
		String sql = "SELECT * FROM word2vec";
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				String word = rs.getString(1);
				List<Double> list = new ArrayList<>();
				for (int i = 2; i <= 201; i++) {
					double columnValue = rs.getDouble(i);
					list.add(columnValue);
				}
				Cos cos = new Cos(word, list);
				coss.add(cos);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("getGG");
		compareVec(coss);
	}

	public static void readFile(String pathname) throws IOException {
		@SuppressWarnings("resource")
		BufferedReader br = new BufferedReader(new FileReader(new File(pathname)));
		String temp = "";
		List<Cos> coss = new ArrayList<>();
		while ((temp = br.readLine()) != null) {
			if (temp.length() < 100) {
				continue;
			}
			String[] ss = temp.split("\\s+");
			String word = ss[0];
			List<Double> list = new ArrayList<>();
			for (int i = 1; i < ss.length; i++) {
				list.add(Double.parseDouble(ss[i]));
			}
			Cos cos = new Cos(word, list);
			coss.add(cos);
		}
		System.out.println("getGG");
		compareVec(coss);
	}

	public final static void compareVec(List<Cos> list) {
		int size = list.size();
		for (int i = 0; i < size; i++) {
			for (int j = i + 1; j < size; j++) {
				Cos cos1 = list.get(i);
				Cos cos2 = list.get(j);
				calculate(cos1, cos2);
			}
		}
	}

	/**
	 * @Title: calculate
	 * @Description: TODO 计算俩个词向量的余弦值
	 * @param cos1
	 * @param cos2
	 * @return void
	 * @author HeYan
	 */
	public final static void calculate(Cos cos1, Cos cos2) {
		List<Double> v1 = cos1.getValues();
		List<Double> v2 = cos2.getValues();
		double res = 0;
		// 200维度
		for (int i = 0; i < 200; i++) {
			res += v1.get(i) * v2.get(i);
		}
		if (res >= 0.5) {
			insertCos(cos1.getWord(), cos2.getWord(), res);
		}
	}

	public static final void insertCos(String w1, String w2, double cos) {
		PreparedStatement ps = null;
		String sql = "INSERT INTO cos_20161125(cos_word1,cos_word2,cos_value) VALUES(?,?,?)";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, w1);
			ps.setString(2, w2);
			ps.setDouble(3, cos);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	@SuppressWarnings("static-access")
	public static void main(String[] args) throws IOException {
		CosDao dao = new CosDao();
		dao.readFile("/var/server/java/heyan/vectors1122.txt");
	}
}
